package com.stationbuilding.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.stationbuilding.R;
import com.stationbuilding.bean.DictBean;
import com.stationbuilding.bean.TimeSpinnerBean;
import java.util.List;

public class TimeSpinnerAdapter extends ArrayAdapter<DictBean> {

    private Context mContext;
    private int resourceId;
    private int dropDownResourceId;
    private List<DictBean> timeList;

    public TimeSpinnerAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<DictBean> list) {
        super(context, resource, list);
        this.mContext = context;
        this.resourceId = resource;
        this.timeList = list;
    }

    public void setTimeList(List<DictBean> list) {
        timeList = list;
    }

    public void setDropDownResource(int dropDownResourceId) {
        super.setDropDownViewResource(dropDownResourceId);
        this.dropDownResourceId = dropDownResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.timeText = (TextView) view.findViewById(R.id.text1);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.timeText.setText(timeList.get(position).getName());
        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(dropDownResourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.timeText = (TextView) view.findViewById(android.R.id.text1);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.timeText.setText(timeList.get(position).getName());
        return view;
    }

    @Override
    public DictBean getItem(int position) {
        return timeList.get(position);
    }

    @Override
    public int getCount() {
        return timeList.size();
    }

    private class ViewHolder{
        TextView timeText;
    }
}
